﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Common.EventHandler;
using Microsoft.Xna.Framework;

namespace UIComponentSprite.Building.Menu
{
    /// <summary>
    /// 菜单单元块
    /// </summary>
    public class MenuSprite:UserControlledSprite
    {
        public List<MenuItemSprite> Items=new List<MenuItemSprite>();//菜单项
        public MenuItemSprite ParentItemSprite;//所属父菜单子项
        public static short BuildingId;//建筑Id
        public MenuSprite(string[] items)
        {
            foreach (var item in items)
            {
                MenuItemSprite mi = new MenuItemSprite(item);
                this.Items.Add(mi);
                mi.MenuSprite = this;
            }
            foreach (var item in this.Items)
            {
                item.ClickLeftEvent += new ClickLeftEventHandler(this.SetShowing);
            }
        }
        public override void Draw(GameTime gameTime)
        {
            if (!Showing)
                return;
            foreach (var item in Items)
            {
                item.Draw(gameTime);
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (!Showing)
                return;
            int y = DrawPosition.Y;
            int h = Items.First().DrawPosition.Height;
            for (int i = 0; i < Items.Count; i++)
            {
                if(i>0)
                    y += h;
                Items[i].Update(gameTime, new Point(DrawPosition.X, y));//传递绘制位置，Y坐标递增
            }
        }

        /// <summary>
        /// 父菜单子项鼠标移入事件处理器
        /// </summary>
        /// <param name="args"></param>
        public void MoveOnEventHandlerByParentItem(object args)
        {
            ClickLeftEventHandler(args);
        }
        /// <summary>
        /// 鼠标左键单击事件处理器
        /// </summary>
        /// <param name="args"></param>
        public void ClickLeftEventHandler(object args)
        {
            string[] arr = args.ToString().Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            SetShowing(arr[0]=="1");
            string[] point = arr[1].Split("|".ToCharArray());
            DrawPosition.X = Convert.ToInt32(point[0]);
            DrawPosition.Y = Convert.ToInt32(point[1]);
            if(arr.Length>=3)
            BuildingId = Convert.ToInt16(arr[2]);
            //计算菜单宽高，方便判断鼠标事件是否位于其中
            MenuItemSprite item = Items.First();
            DrawPosition.Width = item.DrawPosition.Width;
            DrawPosition.Height = item.DrawPosition.Height*Items.Count;

        }
    }
}
